//
//  ChannelAdapterAction.m
//  HdStore
//
//  Created by zhenqiu on 16/4/28.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "ChannelAdapterAction.h"
#import "CacheHelper.h"
#import "NSDate+Utilities.h"

@implementation ChannelAdapterAction
@synthesize cacheDic;

-(instancetype)init{
    
    if(self = [super init]){
        
        cacheDic = nil;
    }
    
    return self;
    
}


-(void)start{
    
//    cacheDic = [[CacheHelper getInstance] getCacheValue:USER_SERVER_INPUT_INFO];
    cacheDic = [[CacheHelper getInstance] getCacheValue:USER_LOCAL_INPUT_INFO];
    
}


-(id)genDataFromCache:(id)channelDic{
    
    NSMutableDictionary *cacheCommitDic = [NSMutableDictionary dictionaryWithCapacity:15];
    [self genDataFromCacheRecur:channelDic withOutDic:cacheCommitDic];
    return cacheCommitDic;
    
}

-(void)genDataFromCacheRecur:(id)recurArray  withOutDic:(id) outDic{
    
    if (self.cacheDic) {
        
        for (NSInteger i  = 0; i < [recurArray count]; i++ ) {
            
            NSDictionary * sectionDic = [recurArray objectAtIndex:i];
            NSArray * itemArray = [sectionDic valueForKey:@"subs"];
            
            if([[itemArray objectAtIndex:0] isKindOfClass:[NSDictionary class]]){
                
                [self genDataFromServerRecur:itemArray withOutDic:outDic];
            }else{
                
                for (NSInteger j = 0; j < [itemArray count]; j++) {
                    
                    DataModle * item = (DataModle*)[itemArray objectAtIndex:j];
                    NSMutableDictionary * itemDic = [self.cacheDic valueForKey:item.key];
                    
                    if(itemDic && [[itemDic allKeys] count] > 0)
                        [outDic setValue:itemDic forKey:item.key];
                    
                }
                
            }
            
        }
        
    }

    
}



-(id)genDataFromServer:(id)channelDic{
    
    NSMutableDictionary *cacheCommitDic = [NSMutableDictionary dictionaryWithCapacity:15];
    [self genDataFromServerRecur:channelDic withOutDic:cacheCommitDic];
    return cacheCommitDic;

}


-(void)genDataFromServerRecur:(id)recurArray  withOutDic:(id) outDic{
    
    if (self.cacheDic) {
        
        for (NSInteger i  = 0; i < [recurArray count]; i++ ) {
            
            NSDictionary * sectionDic = [recurArray objectAtIndex:i];
            NSArray * itemArray = [sectionDic valueForKey:@"subs"];
            
            if([[itemArray objectAtIndex:0] isKindOfClass:[NSDictionary class]]){
                
                [self genDataFromServerRecur:itemArray withOutDic:outDic];
            }else{
                
                for (NSInteger j = 0; j < [itemArray count]; j++) {
                    
                    DataModle * item = (DataModle*)[itemArray objectAtIndex:j];
                    NSMutableDictionary * itemDic = [self genData:item withDic:self.cacheDic];
                    
                    if([[itemDic allKeys] count] > 0)
                        [outDic setValue:itemDic forKey:item.key];
                    
                }

            }
            
        }
        
    }

    
}


-(id)genData:(DataModle*) item withDic:(id) dic{
    
    NSString * key = item.key;
    ItemDataType ltype = item.type;
    
    NSMutableDictionary * itemDic = [[NSMutableDictionary alloc] initWithCapacity:2];
    
    switch (ltype) {
            
        case DATA_TYPE_LIST:
        {
            
            NSString * content = @"";
            ItemListData * listItem = (ItemListData*)item;
            NSString * datasource = listItem.datasource;
            id cid = [dic valueForKey:key];
            
            if(cid && [cid integerValue] > 0){
                
                NSInteger cidNum = [cid integerValue];
                content =  [[GetJsonData standardGetInstance] getLoanJsonItemData:datasource withId:cidNum];
                [itemDic setValue:content forKey:@"data"];
                [itemDic setValue:cid forKey:@"id"];
                [itemDic setValue:key forKey:@"key"];
                
            }
            
        }
            break;
            
        case DATA_TYPE_IMAGE:
        {
            
            NSString * url = [dic valueForKey:key];
            if(url && [url length] > 0){
                
                [itemDic setValue:@"上传成功" forKey:@"data"];
                [itemDic setValue:url forKey:@"id"];
            }
            
        }
            break;
            
        case DATA_TYPE_TEXT:
        case DATA_TYPE_TEL:
        {
            NSString * content  = [dic valueForKey:key];
            if(content && [content length] > 0){
                
                [itemDic setValue:content forKey:@"data"];
                [itemDic setValue:key forKey:@"key"];
            }
            
        }
            break;
            
        case DATA_TYPE_RADIO:{
            
            id data = [dic valueForKey:key];
            if(data){
                
                [itemDic setValue:data forKey:@"data"];
                [itemDic setValue:key forKey:@"key"];
            }
            
        }
            break;
        case DATA_TYPE_NUM:
        case DATA_TYPE_PHONE:
        case DATA_TYPE_UNIT:
        {
            
            id numData = [dic valueForKey:key];
            if(!numData) break;
            if([numData isKindOfClass:[NSString class]]){  // long num so str
                
                if([numData length] > 0){
                    
                    [itemDic setValue:numData forKey:@"data"];
                    [itemDic setValue:key forKey:@"key"];
                }
                
            }else{
                
                NSInteger num = [numData integerValue];
                
                if(num > 0){
                    
                    NSString * numStr = [NSString stringWithFormat:@"%ld",(long)num];
                    [itemDic setValue:numStr forKey:@"data"];
                    [itemDic setValue:key forKey:@"key"];
                }
                
            }
            
        }
            break;
            
        case DATA_TYPE_DATE:
        {
            
            id numData = [dic valueForKey:key];
            if(!numData) break;
            
            NSInteger num = [numData integerValue];
            NSDate * date = [NSDate dateWithTimeIntervalSince1970:num];
            NSString * format = @"yyyy-MM-dd";
            NSString * content = [date stringWithFormat:format];
            [itemDic setValue:numData forKey:@"id"];
            [itemDic setValue:content forKey:@"data"];
            
        }
            break;
            
        case DATA_TYPE_CITY:
        {
            
            NSString * content = @"";
            id cid = [dic valueForKey:key];
            
            if(cid && [cid integerValue] > 0){
                
                NSInteger cidNum = [cid integerValue];
                content =  [[GetJsonData standardGetInstance] getCityJsonItemData:key withId:cidNum];
                
                [itemDic setValue:content forKey:@"data"];
                [itemDic setValue:cid forKey:@"id"];
                [itemDic setValue:key forKey:@"key"];
            }
            
        }
            
        default:
            break;
    }
    
    return itemDic;
    
}


@end
